package com.lksoft.bz.action;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import com.lksoft.bz.Bz;
import com.lksoft.bz.StitchPatternSerializer;

public class SaveAsAction extends AbstractAction {
	private static final long serialVersionUID = -1398267318395558651L;

	// The program
	protected Bz bz;
	
	/**
	 * @param bz The program
	 */
	public SaveAsAction(Bz bz){
		super();
		this.bz = bz;
		
		putValue(MNEMONIC_KEY, KeyEvent.VK_A);
	}

	/**
	 * For subclasses
	 * @param name
	 * @param icon
	 */
	protected SaveAsAction(String name, Icon icon) {
		super(name, icon);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		showSaveDialog();
	}
	
	/**
	 * Save file to current file, or ask user if no current file
	 * @return true-If user saved  false-If user canceled
	 */
	public boolean doSave(){
		return showSaveDialog();
	}
	
	/**
	 * Save file chooser dialog
	 * @return true-If user saved  false-If user canceled
	 */
	public boolean showSaveDialog() {
		JFileChooser chooser = new JFileChooser();
		chooser.setMultiSelectionEnabled(false);
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		chooser.setFileFilter(new FileFilter() {
			@Override
			public String getDescription() {
				return "Stitch pattern(*"+StitchPatternSerializer.SFILE_EXT+")";
			}
			@Override
			public boolean accept(File f) {
				return f.isDirectory() || f.getName().endsWith(StitchPatternSerializer.SFILE_EXT);
			}
		});
		int res = chooser.showSaveDialog(bz.getFrame());
		
		switch( res ){
		case JFileChooser.APPROVE_OPTION:
			return save(chooser.getSelectedFile());
		case JFileChooser.CANCEL_OPTION:
			return false;
		}
		
		return false;
	}

	/**
	 * Saves current stitch pattern to specified file
	 * @param file
	 */
	protected boolean save(File file) {
		// Set extension to current file format
		if( !file.getName().endsWith(StitchPatternSerializer.SFILE_EXT)){
			// If already have an extension, replace it
			int dotPos = file.getName().lastIndexOf('.');
			if( dotPos != -1 ){
				String path = file.getAbsolutePath().substring(0, file.getAbsolutePath().length()-dotPos) + StitchPatternSerializer.SFILE_EXT;
				file = new File(path);
			} else {
				file = new File(file.getAbsolutePath()+StitchPatternSerializer.SFILE_EXT);
			}
		}
		
		// If file exists, ask confirmation
		if( file.exists() && !file.equals(bz.getCurrentFile()) ){
			int res = JOptionPane.showConfirmDialog(bz.getFrame(), Bz.getUIString("messages.confirm.overwrite"));
			if( res != JOptionPane.YES_OPTION ) return false;
		}
		
		if( !StitchPatternSerializer.writeStitchPattern(file, bz.getStitchPanel().getStitchPattern()) ){
			JOptionPane.showMessageDialog(bz.getFrame(), Bz.getUIString("messages.errors.cantsave"));
			return false;
		}
		
		// Notify history
		bz.onSave(file);
		return true;
	}
}
